(in-package #:org.apache.zookeeper.audit)

(defclass AuditEvent ()
  (;;(PAIR_SEPARATOR :type character :initform #\t) ;; TODO The value #\t is not of type SEQUENCE
   (PAIR_SEPARATOR :type string :initform "\t")
   (KEY_VAL_SEPARATOR :type string :initform "=" )
   (logEntries :type hash-table :initform (make-hash-table))
   (result :type Result :initarg :result))) ;; TODO shoud have a type check here














;; 44
(defmethod addEntry ((clz AuditEvent) fieldName value)
  (if (not (null value))
      (let ((logEntries (slot-value clz 'logEntries)))
	(setf (gethash (String fieldName) logEntries) value))))

;; 64
;; @override
(defmethod toString((clz AuditEvent))
  (let ((buffer nil)
	(first 'T)
	(logEntries (slot-value clz 'logEntries)))
    (loop for key being the hash-keys of logEntries
        using (hash-value value)
	  do (if (not (null value))
		 (progn
		   (if (string-equal first 'T)
		       (setf first 'nil)
		       (setf buffer (concatenate 'string buffer (slot-value clz 'PAIR_SEPARATOR))))
		   (setf buffer (concatenate 'string buffer key (slot-value clz 'KEY_VAL_SEPARATOR) value)))))
    (if (> (length buffer) 0)
	(setf buffer (concatenate 'string buffer (slot-value clz 'PAIR_SEPARATOR))))
    (setf buffer (concatenate 'string buffer "result" (slot-value clz 'KEY_VAL_SEPARATOR) (string (slot-value clz 'result))))
    buffer))


;; 90
(deftype FieldName ()
  '(member :USER :OPERATION :IP :ACL :ZNODE :SESSION :ZNODE_TYPE))


;; 94
(deftype Result ()
  '(member :SUCCESS :FAILURE :INVOKED))
